//
//  HZPeiziAddMoneyVC.m
//  Stock_Stategy
//
//  Created by MayerF on 2017/11/29.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZPeiziAddMoneyVC.h"
#import "HZPeiziAddMoneyView.h"

@interface HZPeiziAddMoneyVC ()
@property (nonatomic, strong)HZPeiziAddMoneyView *pAddMoneyView;
@property (nonatomic, copy)NSString *pAccountId;
@property (nonatomic, strong)HZPeiziAccountMoneyModel *pMoneyModel;
@property (nonatomic, copy)NSString *pEableMoney;
@end

@implementation HZPeiziAddMoneyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kMYHexToRGB(0xf6f6f6, 1);
    self.title = @"追加保证金";
    [self setRightBarBtnItemImage:nil title:@"追加"];
    
    _pAddMoneyView = [HZPeiziAddMoneyView new];
    [self.view addSubview:_pAddMoneyView];
    [_pAddMoneyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    NSDictionary *dict = self.params;
    if ([dict.allKeys containsObject:@"money"]) {
        _pMoneyModel = dict[@"money"];
    }
    if ([dict.allKeys containsObject:@"accountId"]) {
        _pAccountId = dict[@"accountId"];
    }
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self p_requestPeiziMoney];
    [self p_requestPeiziMoneyList];
    [_pAddMoneyView updateMoneyData:_pMoneyModel];
}
- (void)rightBarBtnOnClick {
    NSString *money = [_pAddMoneyView getOutMoney];
    if ([money isEqual:@""]) { return;}
    NSString *message = [NSString stringWithFormat:@"确定要追加%@元吗？", money];
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self p_requestPeiziaddMoneyActionWithMoney:money];
    }];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertVC addAction:actionOk];
    [alertVC addAction:actionCancel];
    //    NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:@"声明"];
    //    [alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
    //    [alertVC setValue:alertControllerStr forKey:@"attributedTitle"];
    [self presentViewController:alertVC animated:YES completion:nil];
}

- (void)p_requestPeiziMoney
{
    [SVProgressHUD show];
    [HZApiManager apiPostParams:@{kAPIFuctionKey: @"300004"} success:^(id objc) {
        if ([objc isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dict = (NSDictionary *)objc;
            if ([dict.allKeys containsObject:@"enable_balance"]) {
                _pEableMoney = dict[@"enable_balance"];
            }
        }
        if (_pEableMoney) {
            [_pAddMoneyView updateEableMoney:_pEableMoney];
        }
        [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.domain];
    }];
}
- (void)p_requestPeiziMoneyList
{
    [SVProgressHUD show];
    [HZApiManager apiPostParams:@{kAPIFuctionKey: @"300035", @"contract_id": _pAccountId, @"business_type": @"11"} success:^(id objc) {
        if ([objc isKindOfClass:[NSArray class]]) {
            NSMutableArray *tempArray = [NSMutableArray array];
            for (NSDictionary *dict in objc) {
                HZPeiziMoneyOutListModel *model = [HZPeiziMoneyOutListModel mj_objectWithKeyValues:dict];
                [tempArray addObject:model];
            }
            [_pAddMoneyView p_updateNewData:tempArray];
        }
        [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.domain];
    }];
}
- (void)p_requestPeiziaddMoneyActionWithMoney:(NSString *)money
{
    [SVProgressHUD show];
    [HZApiManager apiPostParams:@{kAPIFuctionKey: @"300033", @"contract_id": _pAccountId, @"occur_balance": money} success:^(id objc) {
        [SVProgressHUD showSuccessWithStatus:@"追保申请提交成功"];
        [_pAddMoneyView clearMoney];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self p_requestPeiziMoney];
            [self p_requestPeiziMoneyList];
        });
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.domain];
    }];
}

@end
